package com.zerui.manager.icc.service;

import com.alibaba.cola.exception.BizException;
import com.alibaba.fastjson.JSONObject;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.icc.exception.ClientException;
import com.zerui.manager.icc.dto.CarInfoVO;
import com.zerui.manager.icc.properties.ICCProperties;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("iccCarInfoService")
public class CarInfoService extends AbstractBase {

    @Resource
    private ICCProperties iccProperties;

    /**
     * 新增车辆信息（基本信息）
     *
     * @param carInfoVO 车辆信息
     * @return
     * @throws ClientException
     */
    public CarInfoVO add(CarInfoVO carInfoVO) throws ClientException {
        Assert.notNull(carInfoVO.getCarNum(), "车牌号不能为空");
        Assert.notNull(carInfoVO.getDepartmentId(), "所属部门不能为空");
        this.generalRequest(this.iccProperties.getCar().getAddUrl(), Method.POST, carInfoVO, "data");
        return carInfoVO;
    }

    /**
     * 更新车辆信息
     *
     * @param carInfoVO 车辆信息
     * @return
     * @throws ClientException
     */
    public Boolean update(CarInfoVO carInfoVO) throws ClientException {
        Assert.notNull(carInfoVO.getCarNum(), "车牌号不能为空");
        Assert.notNull(carInfoVO.getDepartmentId(), "所属部门不能为空");
        this.generalRequest(this.iccProperties.getCar().getUpdateUrl(), Method.PUT, carInfoVO, "data");
        return true;
    }

    /**
     * 删除指定车辆编号的车辆信息
     *
     * @param carCodes 车辆编号
     * @return
     * @throws ClientException
     */
    public Boolean delete(String... carCodes) throws ClientException {
        Assert.notNull(carCodes, "请指定车辆编号");
        Map<String, Object> joParam = new HashMap<>();
        joParam.put("carCodeList", Arrays.asList(carCodes));
        this.generalRequest(this.iccProperties.getCar().getDeleteUrl(), Method.POST, joParam, "data");
        return true;
    }

    /**
     * 查询指定车辆编号的车辆信息
     *
     * @param carCode 车辆编号
     * @return
     * @throws ClientException
     */
    public CarInfoVO selectByCarCode(String carCode) throws ClientException {
        Assert.notNull(carCode, "请指定车辆编号");
        Map<String, String> joParam = new HashMap<>();
        joParam.put("carCode", carCode);
        joParam.put("domainId", "");
        JSONObject joData = this.generalRequest(this.renderUrl(this.iccProperties.getCar().getDetailUrl(), joParam), Method.GET, null, "data");
        List<CarInfoVO> carInfoVOS = this.convert2TypeData(joData, CarInfoVO.class);
        return carInfoVOS.size() > 0 ? carInfoVOS.get(0) : null;
    }

    /**
     * 上传照片
     *
     * @param file 照片文件
     * @return
     */
    public String uploadImg(MultipartFile file) {
        try {
            JSONObject joData = this.generalUpload(iccProperties.getCar().getUploadImgUrl(), Method.POST, file.getName(), file.getBytes(), "data");
            return joData.getString("fileUrl");
        } catch (Exception e) {
            throw new BizException("上传车辆照片出错", e);
        }
    }
}
